package com.hse.gui;

import com.hse.network.exceptions.NetworkException;
import com.hse.SensorAnalyser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: nowarranty
 * Date: 24.05.2009
 * Time: 18:46:48
 * To change this template use File | Settings | File Templates.
 */
public class MainFrame extends JFrame {
    Form form;
    JMenuItem stopNetworkMenuItem;
    JMenuItem startNetworkMenuItem;

    String port="COM7";

    public MainFrame() throws HeadlessException {
        setTitle("Sensor Analyser 3000");
        createFrameMenu();
        form = new Form();
        setContentPane(form.panel1);
    }
    /*
     * Creates main menu for Form
     */
    private void createFrameMenu() {

        JMenuBar jMenuBar = new JMenuBar();
        setJMenuBar(jMenuBar);
        JMenu fileMenu = new JMenu("File");
        jMenuBar.add(fileMenu);
         startNetworkMenuItem = new JMenuItem("Create Network");
        startNetworkMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //TODO: Start network code
                //EndDeviceListener endDeviceListener = new EndDeviceListener(form);
                //endDeviceListener.execute();
                //new Thread(new gui.EndDeviceListener1(form)).start();
                try {
                    Thread.State state=form.sensorAnalyser.getState();
                    if(state==Thread.State.TERMINATED){
                        form.sensorAnalyser=new SensorAnalyser(form);
                       form.sensorAnalyser.start();
                    }
                    form.sensorAnalyser.initCoordinator(port);
                    form.sensorAnalyser.createNetwork();
                    startNetworkMenuItem.setEnabled(false);
                    stopNetworkMenuItem.setEnabled(true);
                    form.log("Network created");
                } catch (InstantiationException e1) {
                    form.showErrorMessage(e1.getLocalizedMessage());
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (NetworkException e1) {
                    form.showErrorMessage(e1.getLocalizedMessage());
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }
        });
        fileMenu.add(startNetworkMenuItem);
        stopNetworkMenuItem = new JMenuItem("Stop Network");
        stopNetworkMenuItem.setEnabled(false);
        stopNetworkMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    form.sensorAnalyser.destroyNetwork();
                    form.sensorAnalyser.closeCoordinator();
                    stopNetworkMenuItem.setEnabled(false);
                    startNetworkMenuItem.setEnabled(true);
                    form.log("Network destroyed");
                } catch (NetworkException e1) {
                    form.showErrorMessage(e1.getLocalizedMessage());
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });
        fileMenu.add(stopNetworkMenuItem);
        fileMenu.addSeparator();
        JMenuItem settingsMenuItem = new JMenuItem("Settings");
        settingsMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                SettingsDialog dialog = new SettingsDialog();
                dialog.setPort(port);
                dialog.pack();
                dialog.setVisible(true);
                port=dialog.getPort();
            }
        });
        fileMenu.add(settingsMenuItem);
        fileMenu.addSeparator();
        JMenuItem exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                try {
                    form.sensorAnalyser.destroyNetwork();
                } catch (NetworkException e1) {
                    e1.printStackTrace();
                }
                System.exit(0);
            }
        });
        fileMenu.add(exitMenuItem);
        JMenu helpMenu = new JMenu("Help");
        jMenuBar.add(helpMenu);
        JMenuItem whatisitItem = new JMenuItem("What is it?");
        whatisitItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "This is Sensor Anal yser 3000.\n" +
                        "-Insert right com port address in \"Settings\"\n"+
                        "-Select \"Create Netwok\" from \"File\" menu to create new sensor network\n" +
                        "-Connect your devices to this network and they will appear in \"Main area\"\n" +
                        "-Enjoi sending and recieving data to and from your devices\n" +
                        "-You can choose one or more devices in the \"Attached devices\" menu\n" +
                        "-You can explore log of your work in \"Log\" area\n" +
                        "-Select \"Stop Netwok\" to destroy it.\n");
            }
        });
        helpMenu.add(whatisitItem);
        JMenuItem aboutMenuItem = new JMenuItem("About");
        aboutMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                AboutDialog dialog = new AboutDialog();
                dialog.pack();
                dialog.setVisible(true);
            }
        });
        helpMenu.add(aboutMenuItem);


    }


    /*
    * Main method
    */

    public static void main(String[] args) {
        MainFrame frame = new MainFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(1200, 700);
        frame.setVisible(true);
    }
}
